package Classes.Managers// ActionScript file
{
	import Classes.Interfaces.I_NeedSWFSource;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	
	/**
	 *swf资源载入类 
	 * @author upstream
	 * 
	 */	
	public class M_LoadSWFSource extends Manager implements I_NeedSWFSource
	{
		private static var _instance:M_LoadSWFSource;
		private var _ownerResourceObj:Object;
		private var _waitingLoadTargetsAry:Array;
		private var _isLoading:Boolean;
		private var _mainPath:String;
		private var _smallLoadingPath:String;
		public function M_LoadSWFSource():void
		{
			_instance=this;
			this.init();
		}
		
		private function init():void
		{
			this._ownerResourceObj=new Object;
			this._waitingLoadTargetsAry=new Array;
		}
		
		override public function importData():void
		{
			this._mainPath=M_ConfigData.instance.imgUrl;
			
			var UISourcePath:String=M_ConfigData.instance.uiPackageUrl;
			this.needLoad(this,UISourcePath,"loadMainUI",true);
			
			this.smallLoadingPath=M_ConfigData.instance.goFriendHomeLoadingUrl;
			//			this.needLoad(this,smallLoadingPath,"smallLoading",true);
		}
		
		public function needLoad(loadTarget:I_NeedSWFSource,loadPath:String,sourceName:String,sign:Boolean=false):void
		{
			if(this.checkSource(sourceName))
			{
				loadTarget.sourceLoadOver(sourceName);
				return;
			}
//			var path:String=sign?loadPath:(this.mainPath+loadPath);
			var path:String=loadPath;
			var newNeedLoadTarget:Object={loadTarget:loadTarget,sourceName:sourceName,loadPath:path};
			this.waitingLoadTargetsAry.push(newNeedLoadTarget);
			this.loadCheck();
		}
		
		private function loadCheck():void
		{
			if(!this.isLoading && this.waitingLoadTargetsAry.length)
			{
				var needLoadTargetObj:Object=this.waitingLoadTargetsAry[0];
				if(this.checkSource(needLoadTargetObj.sourceName))
				{
					this.waitingLoadTargetsAry.shift();
					(needLoadTargetObj.loadTarget as I_NeedSWFSource).sourceLoadOver(needLoadTargetObj.sourceName);
					this.loadCheck();
				}
				else
				{
					this.isLoading=true;
					this.beginLoad(needLoadTargetObj.loadPath);
				}
			}
		}
		
		private function beginLoad(sourcePath:String):void
		{
			trace("开始加载"+sourcePath);
			var loader:Loader=new Loader();
			var request:URLRequest=new URLRequest(sourcePath+ZombieTown.instance.pageParams);
			var context:LoaderContext=new LoaderContext(false,ApplicationDomain.currentDomain);
//			if(M_ConfigData.instance.isOnLocalHost==1)
//			{
//				context=new LoaderContext(false,ApplicationDomain.currentDomain);
//			}else{
//				context= new LoaderContext(true, ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
//			}
//			var context:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
			if(null!=ZombieTown.instance.callloadProcessBarHandler)
			{
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, ZombieTown.instance.callloadProcessBarHandler);
			}
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadOverHandler);
			loader.load(request,context);
		}
		
		private function loadOverHandler(e:Event):void
		{
			this.isLoading=false;
			if(null!=ZombieTown.instance.callloadProcessBarHandler)
			{
				(e.target as LoaderInfo).removeEventListener(ProgressEvent.PROGRESS, ZombieTown.instance.callloadProcessBarHandler);
				ZombieTown.instance.callloadProcessBarHandler=null;
			}
			(e.target as LoaderInfo).removeEventListener(Event.COMPLETE,loadOverHandler);
			var nowLoadTargetObj:Object=this.waitingLoadTargetsAry.shift();
			this.ownerResourceObj[nowLoadTargetObj.sourceName]=true;
			this.loadCheck();
			(nowLoadTargetObj.loadTarget as I_NeedSWFSource).sourceLoadOver(nowLoadTargetObj.sourceName);
		}
		
		public function sourceLoadOver(sourceName:String):void
		{
			this.importOver();
		}
		
		public function checkSource(sourceName:String):Boolean
		{
			if(this.ownerResourceObj[sourceName])return true;
			return false;
		}
		
		/*getter & setter*/
		public static function get instance():M_LoadSWFSource
		{
			return _instance;
		}
		
		public function get ownerResourceObj():Object
		{
			return _ownerResourceObj;
		}
		
		public function get waitingLoadTargetsAry():Array
		{
			return _waitingLoadTargetsAry;
		}
		
		public function get isLoading():Boolean
		{
			return _isLoading;
		}
		
		public function get mainPath():String
		{
			return _mainPath;
		}
		
		public function set isLoading(value:Boolean):void
		{
			_isLoading = value;
		}
		
		public function get smallLoadingPath():String
		{
			return _smallLoadingPath;
		}
		
		public function set smallLoadingPath(value:String):void
		{
			_smallLoadingPath = value;
		}
		
		
	}
}